package com.cnswhy.cloud.server.comsumer.consumer;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.utils.CnswhyUtil;
import com.cnswhy.cloud.common.logging.uitls.MDCUtil;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.enumeration.TransType;
import com.cnswhy.cloud.server.common.entity.enumeration.WalletType;
import com.cnswhy.cloud.server.common.entity.mq.SpellFinishedMsg;
import com.cnswhy.cloud.server.common.entity.mq.SpellInfoMsg;
import com.cnswhy.cloud.server.common.entity.mq.SpellMsg;
import com.cnswhy.cloud.server.comsumer.event.SpellFinishedEvent;
import com.cnswhy.cloud.server.core.entity.*;
import com.cnswhy.cloud.server.core.service.CustomerLuckCountService;
import com.cnswhy.cloud.server.core.service.IntegralLogService;
import com.cnswhy.cloud.server.core.service.customer.CustomerPrivilegeService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.service.shop.ShopProductService;
import com.cnswhy.cloud.server.core.service.spell.SpellOrderService;
import com.cnswhy.cloud.server.core.service.spell.SpellPriceService;
import com.cnswhy.cloud.server.core.wallet.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
@Slf4j
public class SpellOrderConsumer implements SpellFinishedEvent {
    @Autowired
    SpellPriceService spellPriceService;
    @Autowired
    IdGeneratorService idGeneratorService;
    @Autowired
    SpellOrderService spellOrderService;
    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerPrivilegeService customerPrivilegeService;
    @Autowired
    WalletService walletService;
    @Autowired
    RedisService redisService;
    @Autowired
    ShopProductService shopProductService;
    @Autowired
    IntegralLogService integralLogService;
    @Autowired
    MessageService messageService;
    @Autowired
    CustomerLuckCountService customerLuckCountService;
    @Override
    public void receive(SpellFinishedMsg spellFinishedMsg) {
        spellOrder(spellFinishedMsg);

    }
    private void spellOrder(SpellFinishedMsg spellFinishedMsg){
        log.info("新团:{}", JSON.toJSONString(spellFinishedMsg));
        SpellPrice spellPrice = spellPriceService.getById(spellFinishedMsg.getSpellPriceId());
        List<SpellOrder> spellOrders = spellOrderService.list(Wraps.<SpellOrder>lbQ().eq(SpellOrder::getStatus,2).eq(SpellOrder::getPriceId,spellFinishedMsg.getSpellPriceId())); //拼团中订单
        Long id = null;
        for(SpellOrder spellOrder : spellOrders){
            if(id == null){
                id = spellOrder.getId();
            }
            if(spellOrder.getSpellOrderId().compareTo(spellPrice.getNewSpellOrderId()) < 0 ){
                spellOrder.setSpellOrderId(spellPrice.getNewSpellOrderId());
                spellOrderService.updateById(spellOrder);
            }
        }
        if(id != null){
            SpellMsg spellMsg = SpellMsg.builder().id(id).build();
            spellMsg.setTraceId(MDCUtil.getTraceId());
            messageService.sendQueueByObject(AmqpExchange.SPELL_MESSAGE,spellMsg);
        }
    }

}
